<?php
ArrowController :: importModel("access.ArrowAccessGroup");
ArrowController :: importModel("access.ArrowAccessException");

/**
 * Arrow access group manager class
 * 
 * @version 1.0
 * @license  GNU GPL
 * @author Artur Kmera <artur.kmera@arrowplatform.org> 
 */
class ArrowAccessManager implements IArrowCacheable{
	
	/**
	 * Constant with conf file
	 */
	const CONF_FILE = "/conf/access-groups-list.xml";
	
	/**
	* Configuration from cache provider
	* 
	* @var Array
	*/
	private $configuration;	
	
	/**
	 * Templates Structure instance
	 * 
	 * @var ArrowAccessManager[]
	 */
	private static  $selfInstances = array();

	/**
	 * Project reference
	 * 
	 * @var ArrowProject
	 */
	private $projectReference;
	
	/**
	 * Projects access groups
	 *
	 * @var ArrowAccesGroup[]
	 */
	private $groups = array();


	/**
	 * Singleton
	 * 
	 * @param ArrowProject $projectReference
	 * @return ArrowAccessManager Default instance
	 */
	public static function getDefault( ArrowProject $projectReference ) {
		if (!isset (self :: $selfInstances[$projectReference->getId()])) {
			self :: $selfInstances[$projectReference->getId()] = new ArrowAccessManager( $projectReference );
		}
		return self :: $selfInstances[$projectReference->getId()];
	}
	
	/**
	 * 
	 * @param $projectReference ArrowProject
	 */
	private function __construct( ArrowProject  $projectReference ){
		$this->projectReference = $projectReference;
		
		$confFile = $projectReference->getPath() . "/" . self :: CONF_FILE;
		$cacheFile = ArrowCacheProvider::getFileCache( $this, $confFile, array( "file_prefix" => $projectReference->getId()."_access" )  );
		require_once $cacheFile;
		$this->configuration = $cache;
		foreach( $this->configuration["groups"] as $key => $group ){
			$this->groups[$key] = new ArrowAccessGroup( $key, $group["name"], $group["members"] );
		}
	}
	
	/**
	 * IArrowCacheable implementation
	 *
	 * @param Array $params
	 * @return Array
	 */
	public function generateCache($params){
		$templatesStruct = $this->projectReference->getTemplatesStructure();
		$beansStruct = $this->projectReference->getBeansStructure();
		$tmp = array();
		$conf = $this->projectReference->getXMLConfig(self :: CONF_FILE);
		$tmp = array();
		$tmp["groups"] = array();
		$tmp["memberConnections"] = array();
		$tmp["memberConnections"]["templates"] = array();
		$tmp["memberConnections"]["beans"] = array();
		foreach( $conf->accessGroups->accessGroup as $group ){
			$id = (int) $group["id"];
			$tmp["groups"][$id]["name"] = (string) $group["name"];
			$tmp["groups"][$id]["description"] = (string) $group["description"];
			
			$tmp["groups"][$id]["members"]["beans"] = array(); 
			foreach( $group->beanMembers->bean as $member ){
				try{
					$beanId = $beansStruct->getBeanIdByMappingPath( (string) $member );
					$tmp["groups"][$id]["members"]["beans"][] = $beanId;
					$tmp["memberConnections"]["beans"][$beanId][] = $id;
				}catch( ArrowBeansException $e){}
			}
			
			$tmp["groups"][$id]["members"]["templates"] = array();
			foreach( $group->templateMembers->template as $member ){
				try{
					$templateId = $templatesStruct->getTemplateIdByMappingPath( (string) $member );
					$tmp["groups"][$id]["members"]["templates"][] = $templateId;
					$tmp["memberConnections"]["templates"][$templateId][] = $id;
				}catch( ArrowTemplatesException $e){}
			}
		}
		return $tmp;
	}
	
	/**
	 * Getter for access groups
	 *  
	 * @return AccessGroup[]
	 */
	public function getGroups(){
		return $this->groups;
	}
	
	/**
	 * Returns AccessGroup
	 * @param $groupId
	 * @return ArrowAccessGroup
	 */
	public function getGroupById( $groupId ){
		if (isset($this->groups[$groupId]) )
			return $this->groups[$groupId];
		else
			return null;	
	}

	/**
	 * 
	 * @param $groupId
	 * @return unknown_type
	 */
	public function getGroupByName( $groupName ){
		foreach ($this->groups as $group ){
			if ($group->getName() === $groupName)
				return $group;
		}
		return null;
	}
	
	/**
	* Checking access to object
	*
	* @param unknown_type $examinedObject
	* @return boolean
	*/
	public function checkTemplateAccess( $examinedObject ){
		if( $examinedObject instanceof ArrowTemplateDescriptor  ){
			$accessGroups = $this->user->getAccessGroups();
			$groupList = ArrowController::$activeProject->getAccessManager()->getGroups();
			foreach( $accessGroups as $accesGroup ){
				$inGroupTemplates = $groupList[$accesGroup]->getTemplates();
				if( in_array($examinedObject->getId(), $inGroupTemplates ) )
					return true;
			}
			return false;
		}
		
		return false;
		if( is_null($this->user) )
			return false;
		//TODO uprawnienia
		return true;
		if( in_array( $this->user[AuthUser::F_ACCESS_GROUP], $accesGroups ) )
			return true;
	}
	
	

}

?>